package com.uchedao.seller.http.request;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.uchedao.seller.http.response.SubmitResponse;
import com.uchedao.seller.util.LogUtil;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hang on 2015/9/25.
 */
public class SubmitRequest extends Request<JsonObject> {
    /**
     * Charset for request.
     */
    private static final String PROTOCOL_CHARSET = "utf-8";

    /**
     * Content type for request.
     */
    private static final String PROTOCOL_CONTENT_TYPE =
            String.format("application/json; charset=%s", PROTOCOL_CHARSET);

    private final Response.Listener<JsonObject> mListener;

    private Gson mGson;

    private Map<String, String> mParams = new HashMap<String, String>();

    public SubmitRequest(Map<String, String> params, String action, Response.Listener<JsonObject> listener,
                         Response.ErrorListener errorListener) {
        super(Method.POST, action, errorListener);
        LogUtil.e("ACTION POST >>>>>>>>>>>>>>>>>>>>>>>>" + action);
        mGson = new Gson();
        mListener = listener;
        mParams = params;
    }

    @Override
    protected void deliverResponse(JsonObject response) {
        mListener.onResponse(response);
    }

    private Map<String, String> headers = new HashMap<String, String>();

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers;
    }

    @Override
    protected Response<JsonObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            LogUtil.e("jsonString", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + jsonString + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            SubmitResponse responseData = mGson.fromJson(jsonString, SubmitResponse.class);
            String result = responseData.code;
            if (TextUtils.isEmpty(result)) {
                LogUtil.e("resolverData", "返回结果不是合法的json格式");
                return Response.error(new VolleyError("返回结果不是合法的json格式"));
            }
            if ("0".equals(result)) {
                return Response.success(null,
                        HttpHeaderParser.parseCacheHeaders(response));
            } else {
                LogUtil.e("resolverData", responseData.msg);
                return Response.error(new VolleyError(responseData.msg));
            }

        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    public String getBodyContentType() {
        return PROTOCOL_CONTENT_TYPE;
    }


    @Override
    public byte[] getBody() {
        JSONObject jsonObject = new JSONObject(mParams);
        String mRequestBody = jsonObject.toString();
        LogUtil.e("POST>>>>>>>>"+mRequestBody);
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, PROTOCOL_CHARSET);
            return null;
        }
    }
//    @Override
//    protected Map<String, String> getParams() throws AuthFailureError {
//        return mParams;
//    }
}
